﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WuDada.Core.Post;
using WuDada.Core.Post.Service;
using WuDada.Core.Post.Domain;
using Common.Logging;
using WuDada.Core.Member;
using WuDada.Core.Member.Service;
using WuDada.Core.Member.Domain;

public partial class map_in : System.Web.UI.Page
{
    private ILog m_Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    private PostFactory m_PostFactory;
    private IPostService m_PostService;
    private MemberFactory m_MemberFactory;
    private IMemberService m_MemberService;
    private SessionHelper m_SessionHelper;

    private int m_ParentNodeId = 40;

    private int m_PostId = 0;
    private int m_NodeId = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        m_PostFactory = new PostFactory();
        m_PostService = m_PostFactory.GetPostService();
        m_MemberFactory = new MemberFactory();
        m_MemberService = m_MemberFactory.GetMemberService();
        m_SessionHelper = new SessionHelper();

        m_PostId = QueryStringHelper.GetInteger("post", 0);

        if (!IsPostBack)
        {            
            if (m_PostId == 0)
            {
                Response.Redirect("discuss.aspx");
            }

            PostVO postVO = m_PostService.GetPostByIdNoLazy(m_PostId);
            if (postVO != null)
            {
                m_NodeId = postVO.Node.NodeId;
                ltlName.Text = postVO.Node.Name;
                ltlContent.Text = postVO.Node.HtmlContent;
                ltlTitle.Text = postVO.Title;
                ltlCreatedDate.Text = postVO.CreatedDate.Value.ToString("yyyy.MM.dd");
                if (!string.IsNullOrEmpty(postVO.HtmlContent))
                {
                    ltlHtmlContent.Text = postVO.HtmlContent.Replace(Environment.NewLine, "<br/>");
                }

                MemberVO memberVO = m_MemberService.GetMemberByLoginId(postVO.CreatedBy);
                if (memberVO != null)
                {
                    ltlCreatedBy.Text = memberVO.Name;
                }
                else
                {
                    ltlCreatedBy.Text = postVO.CreatedBy;
                }

                ltlFeedbackContent.Text = string.Format("<area shape='rect' coords='507,2,583,32' href='feedback_discuss.aspx?post={0}'>", m_PostId);
                if (!string.IsNullOrEmpty(postVO.PicFileName))
                {
                    ltlFile.Text = GetFile(postVO.PicFileName);
                }
                FillList();

                if (m_SessionHelper.Member != null && m_SessionHelper.Member.IsAdmin)
                {
                    btnDelete.Visible = true;
                }
                else
                {
                    btnDelete.Visible = false;
                }
            }
        }
    }

    private void FillList()
    {
        Dictionary<string, string> conditions = new Dictionary<string, string>();
        conditions.Add("ParentPostId", m_PostId.ToString());
        conditions.Add("Flag", "1");
        conditions.Add("Order", "order by p.CreatedDate desc ");

        //分頁
        AspNetPager1.RecordCount = m_PostService.GetPostCount(conditions);        
        int pageIndex = (AspNetPager1.CurrentPageIndex - 1);
        int pageSize = AspNetPager1.PageSize;
        conditions.Add("PageIndex", pageIndex.ToString());
        conditions.Add("PageSize", pageSize.ToString());

        RepeaterList.DataSource = m_PostService.GetPostList(conditions);
        RepeaterList.DataBind();
    }

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        FillList();
    }

    protected void RepeaterList_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemIndex != -1)
        {
            Control ctrl = e.Item;
            PostVO postVO = (PostVO)e.Item.DataItem;

            if (!string.IsNullOrEmpty(postVO.HtmlContent))
            {
                UIHelper.SetLiteralText(ctrl, "ltlHtmlContent", postVO.HtmlContent.Replace(Environment.NewLine, "<br/>"));                
            }

            MemberVO memberVO = m_MemberService.GetMemberByLoginId(postVO.CreatedBy);
            if (memberVO != null)
            {
                UIHelper.SetLiteralText(ctrl, "ltlCreatedBy", memberVO.Name);
            }

            if (m_SessionHelper.Member != null && m_SessionHelper.Member.IsAdmin)
            {
                UIHelper.SetContrlVisible(ref ctrl, "btnDelete", true);
            }
            else
            {
                UIHelper.SetContrlVisible(ref ctrl, "btnDelete", false);
            }
        }
    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        if (m_SessionHelper.Member != null && m_SessionHelper.Member.IsAdmin)
        {
            Dictionary<string, string> conditions = new Dictionary<string, string>();
            conditions.Add("ParentPostId", m_PostId.ToString());

            IList<PostVO> replyList = m_PostService.GetPostList(conditions);
            if (replyList != null && replyList.Count() > 0)
            {
                foreach (PostVO reply in replyList)
                {
                    m_PostService.DeletePost(reply);
                }
            }

            PostVO postVO = m_PostService.GetPostById(m_PostId);
            if (postVO != null)
            {
                m_PostService.DeletePost(postVO);
            }

            ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "js", JavascriptUtil.AlertJSAndRedirect("刪除成功。", "discuss.aspx"), false);
        }
        else
        {
            ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "js", JavascriptUtil.AlertJSAndRedirect("僅有管理員權限才可刪除。", string.Format("discuss_in.aspx?post={0}", m_PostId)), false);
        }
    }
    private string GetFile(string fileName)
    {
        return string.Format("<br/><a href='../../upload/{0}' target='_blank'>附加檔案下載</a>", fileName);
    }
    protected void RepeaterList_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        string cmdName = e.CommandName;
        int postId = int.Parse(e.CommandArgument.ToString());
        PostVO postVO = m_PostService.GetPostById(postId);
        switch (cmdName)
        {
            case "myDel":
                if (m_SessionHelper.Member != null && m_SessionHelper.Member.IsAdmin)
                {
                    m_PostService.DeletePost(postVO);
                    FillList();
                }
                else
                {
                    ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "js", JavascriptUtil.AlertJSAndRedirect("僅有管理員權限才可刪除。", string.Format("discuss_in.aspx?post={0}", m_PostId)), false);
                }
                break;

            default:
                break;
        }        

    }
}